package com.mfhcd.facepay.utils;

import android.annotation.SuppressLint;
import android.text.TextUtils;

import com.mfhcd.facepay.global.AppConfig;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;

/**
 * @Author：张洪顺
 * @E-Mail: zhanghs@mfhcd.com
 * @Date：2019-09-19 14:42
 * @版权所有 © 现代金融控股（成都）有限公司
 * @描述：通用时间/日期处理工具类
 */
public class DateUtils {
    public static String PATTERN_DAY = "yyyy-MM-dd";
    public static String PATTERN_YEAR = "yyyy";
    public static String PATTERN_DATE_CHINA = "yyyy年MM月dd日";
    public static String PATTERN_DAY_NO = "yyyyMMdd";
    public static String PATTERN_TIMESTAMP = "yyyyMMddHHmmssSSS";
    public static String pattern_yyyymmddhhmmss = "yyyyMMddHHmmss";
    public static String PATTERN_FULL = "yyyy-MM-dd HH:mm:ss";
    public static String PATTERN_MONTH = "yyyyMM";
    public static String PATTERN_YYYY_MM = "yyyy-MM";
    public static String PATTERN_YYYY_MM_CHINA = "yyyy年MM月";
    public static final String PATTERN_UTC = "EEE, dd MMM yyyy HH:mm:ss 'GMT'";
    public static final String DATE_DEFAULT_CUTOFF_PREFIX = " 00:00:00";
    public static final String DATE_DEFAULT_CUTOFF_SUFFIX = " 23:59:59";

    public static String getUTCTime() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeZone(TimeZone.getTimeZone("UTC"));
        calendar.setTime(new Date());
        Date time = calendar.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat(PATTERN_UTC, Locale.ENGLISH);
        String dateStr = sdf.format(time);
        LogUtil.d("dateStr = " + dateStr);
        String str = sdf.format(calendar.getTime());
        return str;
    }

    /**
     * 将字符串转还成日期格式
     *
     * @param source
     * @param template
     */
    public static Date String2Date(String source, String template) {
        if (source == null) {
            return null;
        }
        Date date = null;
        SimpleDateFormat sdf = new SimpleDateFormat(template);
        try {
            date = sdf.parse(source);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 将日期装换为String
     *
     * @param date
     * @param template
     */
    public static String Date2String(Date date, String template) {
        String dateResult = "";
        SimpleDateFormat sdf = new SimpleDateFormat(template, Locale.ENGLISH);
        if (date != null) {
            dateResult = sdf.format(date);
        }
        return dateResult;
    }

    public static String getTimeStamp(Date date) {
        String dateResult = "";
        SimpleDateFormat sdf = new SimpleDateFormat(PATTERN_TIMESTAMP, Locale.ENGLISH);
        if (date != null) {
            dateResult = sdf.format(date);
        }
        return dateResult;
    }

    /**
     * 给时间加上几个小时
     *
     * @param date 当前时间 格式：yyyy-MM-dd HH:mm:ss
     * @param hour 需要加的时间
     *
     * @return 格式：yyyyMMddHHmmss
     */
    public static String addDateMinut(Date date, int hour, String pattern) {
        if (date == null) {
            return "";
        }
        //显示输入的日期
        System.out.println("front:" + Date2String(date, PATTERN_FULL));
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        // 24小时制
        cal.add(Calendar.HOUR, hour);
        date = cal.getTime();
        String formatDate = Date2String(date, pattern);
        //显示更新后的日期
        System.out.println("after:" + formatDate);
        cal = null;
        return formatDate;
    }

    /**
     * 获取当前日期的前一个月日期
     * 为了灵活处理格式，这里只设置年月日
     *
     * @param monthIncrement 月份增量，-1：上一月，0本月，1下一月...
     *
     * @return 格式：Date
     */
    public static Date getMonthIncrementDate(int monthIncrement) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        cal.add(Calendar.MONTH, monthIncrement);
        return cal.getTime();
    }

    public static long convertStrToLong(String strDate) {
        return String2Date(strDate, PATTERN_DAY).getTime();
    }

    /**
     * 根据当前传入日期获取日期月第一天、最后一天
     *
     * @param currentDate
     */
    public static List<Date> getFirstAndLastDayOfMonthByDate(Date currentDate) {
        List<Date> dateList = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(currentDate);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        Date firstDayOfMonth = calendar.getTime();
        dateList.add(firstDayOfMonth);
        calendar.add(Calendar.MONTH, 1);
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        Date lastDayOfMonth = calendar.getTime();
        dateList.add(lastDayOfMonth);
        return dateList;
    }

    /**
     * 将 已经格式化的日期 转换成另一只格式化
     */
    public static String convertPattern(String date, String pattern, String newPattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        try {
            return new SimpleDateFormat(newPattern).format(sdf.parse(date));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 将 yyyy年MM月dd日 转换成 yyyy-MM-dd
     */
    public static String convertChina(String date) {
        SimpleDateFormat sdf = new SimpleDateFormat(PATTERN_DATE_CHINA);
        try {
            return new SimpleDateFormat(PATTERN_DAY).format(sdf.parse(date));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 将 yyyyMMdd 转换成 yyyy-MM-dd
     */
    public static String convertConnector(String date) {
        SimpleDateFormat sdf = new SimpleDateFormat(PATTERN_DAY_NO);
        try {
            return new SimpleDateFormat(PATTERN_DAY).format(sdf.parse(date));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    public static int getCurrentYear() {
        return Integer.valueOf(new SimpleDateFormat(PATTERN_YEAR).format(new Date(System.currentTimeMillis())));
    }

    public static String getCurrentYearMonth() {
        return new SimpleDateFormat(PATTERN_YYYY_MM).format(new Date());
    }

    public static String getCurrentDay() {
        return new SimpleDateFormat(PATTERN_DAY).format(new Date());
    }

    public static String getCurrentYearMonthDay() {
        return new SimpleDateFormat(PATTERN_DAY_NO).format(new Date());
    }

    public static long dateStringToStamp(String date) {
        long timeStamp;
        @SuppressLint("SimpleDateFormat") SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date dateSramp;
        try {
            dateSramp = format.parse(date);
        } catch (ParseException e) {
            if (AppConfig.DEBUG) {
                e.printStackTrace();
            }
            return 0;
        }
        timeStamp = dateSramp.getTime() / 1000;
        return timeStamp;
    }

    public static String formatDate(long milliseconds, String pattern) {
        return new SimpleDateFormat(pattern).format(new Date(milliseconds));
    }

    public static String trimHHMMSS(String date) {
        if (!TextUtils.isEmpty(date) && date.length() >= 19) {
            return date.substring(0, date.length() - 9);
        } else {
            return date;
        }
    }

    /**
     * 获得本月的开始时间，即 2012-01-01 00:00:00
     */
    public static String getCurrentMonthStartTime() {
        Calendar c = Calendar.getInstance();
        String currentMonthFirstDay = null;
        try {
            c.set(Calendar.DATE, 1);
            currentMonthFirstDay = Date2String(c.getTime(), PATTERN_DAY) + DATE_DEFAULT_CUTOFF_PREFIX;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return currentMonthFirstDay;
    }

    /**
     * 当前月的结束时间，即 2012-01-31 23:59:59
     */
    public static String getCurrentMonthEndTime() {
        Calendar c = Calendar.getInstance();
        String currentMonthLastDay = null;
        try {
            c.set(Calendar.DATE, 1);
            c.add(Calendar.MONTH, 1);
            c.add(Calendar.DATE, -1);
            currentMonthLastDay = Date2String(c.getTime(), PATTERN_DAY) + DATE_DEFAULT_CUTOFF_SUFFIX;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return currentMonthLastDay;
    }

    /**
     * 获取当前日期 前 / 后N个月的开始时间
     * 当前时间 格式：yyyy-MM-dd 00:00:00
     *
     * @param ago 需要加的月份：
     *            小于0：当前日志往前推N个月
     *            大于0：当前日志往后推N个月
     */
    public static String getCurrentMonthAddTime(int ago) {
        Calendar c = Calendar.getInstance();
        String currentMonthAgo = null;
        try {
            c.add(Calendar.MONTH, ago);
            // 判断当前日志以前取开始时间，往后取截止时间
            if (ago < 0) {
                currentMonthAgo = Date2String(c.getTime(), PATTERN_DAY) + DATE_DEFAULT_CUTOFF_PREFIX;
            } else {
                currentMonthAgo = Date2String(c.getTime(), PATTERN_DAY) + DATE_DEFAULT_CUTOFF_SUFFIX;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return currentMonthAgo;
    }
}
